import Vue from "vue";
import VueRouter from "vue-router";
import LoginPage from "../pages/login/login.vue";
import notFound from "../pages/notFound/index.vue";
import localCache from "../utils/localCache";

Vue.use(VueRouter);

const initRoutes = [
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login",
    roleNames: ["user", "admin"],
    name: "login",
    component: LoginPage,
  },
  {
    path: "/main",
    name: "main",
    component: () => import("../pages/main/index.vue"),
    children: [
      {
        path: "",
        redirect: "system",
      },
      {
        path: "system",
        meta: {
          roleNames: ["user", "admin"],
          url: "/main/system",
        },
        component: () => import("../pages/main/system/index.vue"),
      },
      {
        path: "clusterfirst",
        meta: {
          roleNames: ["admin"],
          url: "/main/clusterfirst",
        },
        component: () => import("../pages/main/cluster/first.vue"),
      },
      {
        path: "clusterscond",
        meta: {
          roleNames: ["user"],
          url: "/main/clusterscond",
        },
        component: () => import("../pages/main/cluster/second.vue"),
      },
      {
        path: "cloudnorth",
        meta: {
          roleNames: ["admin"],
          url: "/main/cloudnorth",
        },
        component: () => import("../pages/main/cloud/north.vue"),
      },
      {
        path: "cloudsouth",
        meta: {
          roleNames: ["user"],
          url: "/main/cloudsouth",
        },
        component: () => import("../pages/main/cloud/south.vue"),
      },
      {
        path: "ipconfig",
        meta: {
          roleNames: ["admin"],
          url: "/main/ipconfig",
        },
        component: () => import("../pages/main/ipCpn/index.vue"),
      },
    ],
  },
  {
    path: "*",
    name: "notFound",
    component: notFound,
  },
];
const router = new VueRouter({
  mode: "hash",
  routes: initRoutes,
});
// 导航守卫
router.beforeEach((to, from, next) => {
  if (to.path !== "/login") {
    const userinfo = localCache.getCache("userinfo");
    if (!userinfo) {
      next("/login");
    }
    // if (to.meta.roleNames.includes(userinfo.roleNames[0])) {
    //   next();
    // } else {
    //   return new Vue().$message({
    //     message: "权限不足",
    //     type: "error",
    //   });
    // }
  }
  next();
});
export default router;
